package com.spit.fammo.ui.frg

import android.os.Bundle
import android.text.InputType
import android.text.method.PasswordTransformationMethod
import android.widget.CompoundButton
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.LogUtils
import com.spit.fammo.databinding.FLoginBinding
import com.spit.amoeba.base.BaseFragment
import com.spit.fammo.R
import com.spit.fammo.api.UIHelper
import com.spit.fammo.ext.showMessage
import com.spit.fammo.util.CacheUtil
import com.spit.fammo.viewmodel.LoginModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author nike
 * @Date 2023/7/5 14:53
 * @Description
 */
class LoginFrg: BaseFragment<LoginModel, FLoginBinding>() {

    val loginModel: LoginModel by viewModels()
    override fun initView(savedInstanceState: Bundle?) {
        addLoadingObserve(loginModel)
        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        val user = CacheUtil.getUser()
        if (user != null && user.LoginID != null){
            mViewModel.username.set(user.LoginID)
        }

    }

    inner class ProxyClick() {

        var onCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
//                mViewModel.isShowPwd.set(isChecked)
                if (isChecked) {
                    mDatabind.etPwd.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                    mDatabind.etPwd.transformationMethod = null
                } else {
                    mDatabind.etPwd.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD
                    mDatabind.etPwd.transformationMethod = PasswordTransformationMethod.getInstance()
                }
                mDatabind.etPwd.setSelection(mDatabind.etPwd.text!!.length)
            }

        fun clear(){
            mViewModel.username.set("")
        }


        fun login(){
            when {
                mViewModel.username.get().isEmpty() -> showMessage(getString(R.string.hiht_))
                mViewModel.password.get().isEmpty() -> showMessage(getString(R.string.hiht_))
                else -> loginModel.login(
                    mViewModel.username.get(),
                    mViewModel.password.get(),
                    this@LoginFrg
                )
            }
        }

        fun toSet(){
            UIHelper.startSettingFrg(nav())
        }

    }

}
